type Response = {
  url: string;
  body: any,
  status: number;
  type: string,
  statusText?: string;
  _data?: any;
  headers?: object,
  ok?: boolean,
  redirected?: boolean,
  bodyUsed?: boolean,
};
type ResponseData = {
  code: number,
  msg: string,
  data: object | object[]
}

export const useRequest = async (url: string, options: object) => {
  const router = useRouter();
  const store = useMainStore();
  const appConfig = useAppConfig();
  const host = window.location.hostname;
  const headers = {
    // Authorization:'Bearer '+store.user?.token,
    // tokenID: store.user?.tokenID || null
  };
  const defaultOptions: object = {
    baseURL: appConfig.app.apiUrl,
    headers: headers,
    onResponse({ response }: { response: Response }) {
      const res = response._data;
      if (res.error === 401) {
        router.replace("/login");
        store.logout()
      }
      else if (res.error !== 0) {
        ElMessage.error(res.errorMessage);
      } else {
        //登录成功操作
      }
    },
    onResponseError({ response }: { response: Response }) {
      console.log("response-error", response);
      const res = response._data;
      if (response.status == 401) {
        router.replace("/login");
        store.logout()
      }
    },
  };
  const newOptions: object = { ...defaultOptions, ...options };
  const loadingInstance = ElLoading.service({ fullscreen: false });
  const { data, pending, refresh } = await useFetch(url, newOptions);
  if (!pending?.value) {
    loadingInstance.close();
  }
  let response = data.value
  return { response, refresh };
};
